﻿// Copyright 2013 Ronald Schlenker, www.technewlogic.de.
// 
// This file is part of Byond.
// 
// Byond is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
// 
// Byond is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Byond.  If not, see <http://www.gnu.org/licenses/>.

using Autofac;
using Byond.AudioMidi;
using Byond.Frontend;
using Jacobi.Vst.Framework;

namespace Byond.Vst
{
	class VstBootstrapper : FrontendBootstrapper, IVstHostRegistrar
	{
		public VstBootstrapper()
		{
			// Byond.Vst
			RegisterAssemblyTypes(typeof(VstBootstrapper).Assembly);

			ContainerBuilder.RegisterInstance(this).As<IVstHostRegistrar>();
			
			ContainerBuilder.RegisterType<VstAudioEngine>().AsSelf().As<IAudio>().As<IAudioInfo>().SingleInstance();
			ContainerBuilder.RegisterType<VstMidi>().AsSelf().As<IMidi>().SingleInstance();

			//ContainerBuilder.RegisterType<WinFormsEditor>().SingleInstance();
			ContainerBuilder.RegisterType<Plugin>().SingleInstance();
			ContainerBuilder.RegisterType<ByondPluginInfo>().SingleInstance();
			ContainerBuilder.RegisterType<PluginAudioProcessor>().SingleInstance();
			ContainerBuilder.RegisterType<PluginParameters>().SingleInstance();

			ContainerBuilder.RegisterType<ByondPluginInfo>();
			ContainerBuilder.RegisterType<PluginPersistence>().SingleInstance();
			
			ContainerBuilder.RegisterType<WpfHost>().SingleInstance();

			Build();

			PluginInfo = Container.Resolve<ByondPluginInfo>();
			PluginInstance = Container.Resolve<Plugin>();
			_pluginPersistence = Container.Resolve<PluginPersistence>();
			WpfHost = Container.Resolve<WpfHost>();
			//Editor = Container.Resolve<WinFormsEditor>();
			ByondControl = Container.Resolve<ByondControl>();

			Logger.Info("VST Bootstrapper Ctor OK");
		}

		private readonly PluginPersistence _pluginPersistence;

		//public WinFormsEditor Editor { get; private set; }
		public ByondControl ByondControl { get; private set; }
		public Plugin PluginInstance { get; private set; }
		public ByondPluginInfo PluginInfo { get; private set; }
		public WpfHost WpfHost { get; private set; }

		public void Open()
		{
			Logger.Info("VST Bootstrapper Open...");

			Logger.Info("VST Bootstrapper InitializePersistence");
			InitializePersistence();
			Logger.Info("VST Bootstrapper InitializeRuntime");
			InitializeRuntime();

			Logger.Info("VST Bootstrapper InitializeUi");
			InitializeUi(ByondControl.Dispatcher);
			Logger.Info("VST Bootstrapper InitializeLibrary");
			InitializeLibrary();
		}

		public void RegisterVstHost(IVstHost host)
		{
			var cb = new ContainerBuilder();
			cb.RegisterInstance(host);

			cb.Update(Container);
		}
	}
}
